const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
const { ModuleFederationPlugin } = require("webpack").container;
const { generateRemote } = require("../../../utils");
const deps = require("../package.json").dependencies;
delete deps["@qtrade/error-catch"];
const { getLessLoaderConfig } = require("./style-rules");
const {
  SanRemoteModulePlugin,
  ListenAssetsJsPlugin,
  updateAppInfo,
} = require("../../../base/config/plugins");
module.exports = {
  context: path.resolve(__dirname, ".."),
  entry: "./src/index.tsx",
  output: {
    filename: '[name].[contenthash:8].bundle.js',
    chunkFilename: '[name].[contenthash:8].chuck.js',
    clean: true,
    path: path.resolve(__dirname, "../dist"),
  },
  resolve: {
    extensions: [".jsx", ".js", ".ts", ".tsx"],
  },
  module: {
    rules: [
      {
        test: /\.[jt]sx?$/,
        include: [
          /node_modules[\\/]@qtrade/,
          path.resolve(__dirname, "../src"),
        ],
        use: [
          {
            loader: "babel-loader",
            options: {
              rootMode: "upward",
            },
          },
        ],
      },
      {
        test: /\.less$/,
        include: [
          /node_modules[\\/]@qtrade/,
          path.resolve(__dirname, "../src"),
        ],
        exclude: [/\.module(s?)\.less$/],
        use: [
          "style-loader",
          "css-loader",
          {
            loader: "less-loader",
            options: {
              lessOptions: {
                javascriptEnabled: true,
              },
            },
          },
        ],
      },
      {
        test: /\.module(s?)\.less$/,
        include: [
          path.resolve(__dirname, "../src"),
          /node_modules[\\/]@qtrade/,
        ],
        use: getLessLoaderConfig({ resources: true, cssModules: true }),
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(png|webp)$/,
        type: "asset/resource",
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./public/index.html",
    }),
    new ModuleFederationPlugin({
      name: "fl",
      filename: "entry.[contenthash:8].js",
      exposes: {
        "./FLxx": "./src/App",
        "./qtrade/error-catch": "@qtrade/error-catch",
        "./qtrade/error-catch/src/index.less":
          "@qtrade/error-catch/src/index.less",
      },
      shared: {
        ...deps,
      },
      remotes: {
        common: generateRemote("common", "."),
        base: generateRemote("base", "."),
        "@/utils": generateRemote("common", "./utils"),
        "@/components": generateRemote("common", "./components"),
        react: generateRemote("common", "./react"),
        "react-dom": generateRemote("common", "./react-dom"),
        "react-router-dom": generateRemote("common", "./react-router-dom"),
      },
    }),
    new SanRemoteModulePlugin(),
    new ListenAssetsJsPlugin((file) => {
      if (file.startsWith("entry")) {
        const fileContext = "/fl/"; //"http://localhost:4001/"
        updateAppInfo("fl", fileContext + file);
        return true;
      }
    }),
  ],
};
